package com.example.andylau96.res.Main;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.andylau96.res.Function.Account.accountFragment;
import com.example.andylau96.res.Function.Account.bAddFragment;
import com.example.andylau96.res.Function.Cashier.checkFragment;
import com.example.andylau96.res.Function.Cashier.erjiFragment;
import com.example.andylau96.res.Function.Cashier.jiesuanFragment;
import com.example.andylau96.res.Function.Cashier.orderFragment;
import com.example.andylau96.res.Function.Cashier.xiaofeiFragment;
import com.example.andylau96.res.R;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by AndyLau96 on 2016/11/22.
 */
public class mainFragment extends Fragment implements View.OnClickListener,AdapterView.OnItemClickListener {
    private View view;
    private String theme;
    private List<View> listview;
    private TextView tab1, tab2, tab3;
    public static TextView time;
    private ViewPager pager;
    private ImageView image_zhuye, image_time, image_setting;
    private LinearLayout page1, page2, page3, main_title, main_content, nav_hander_layou;
    private DrawerLayout drawer;
    private ImageButton main_title_left;
    private GridView gridview;

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.main_title_left)
            drawer.openDrawer(GravityCompat.START);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch(MainActivity.permission)
        {
            case 3:
                if(i==0)
                {
                    FragmentManager fm=getFragmentManager();
                    FragmentTransaction ft=fm.beginTransaction();
                    ft.addToBackStack(null);
                    accountFragment s=new accountFragment();
                    ft.replace(R.id.framelayut,s);
                    MainActivity.now=s;
                    ft.commit();
                }
                if(i==1)
                {
                    FragmentTransaction ft=getFragmentManager().beginTransaction();
                    ft.addToBackStack(null);
                    bAddFragment s=new bAddFragment();
                    ft.replace(R.id.framelayut,s);
                    MainActivity.now=s;
                    ft.commit();
                }
                break;
            case 2:
                if(i==0)
                {
                    FragmentTransaction ft=getFragmentManager().beginTransaction();
                    ft.addToBackStack(null);
                    orderFragment s=new orderFragment();
                    ft.replace(R.id.framelayut,s);
                    MainActivity.now=s;
                    ft.commit();
                }
                if(i==1)
                {
                    FragmentTransaction ft=getFragmentManager().beginTransaction();
                    ft.addToBackStack(null);
                    erjiFragment s=new erjiFragment();
                    ft.replace(R.id.framelayut,s);
                    MainActivity.now=s;
                    ft.commit();
                }
                if(i==2)
                {
                    FragmentTransaction ft=getFragmentManager().beginTransaction();
                    ft.addToBackStack(null);
                    checkFragment s=new checkFragment();
                    ft.replace(R.id.framelayut,s);
                    MainActivity.now=s;
                    ft.commit();
                }
                if(i==3)
                {
                    FragmentTransaction ft=getFragmentManager().beginTransaction();
                    ft.addToBackStack(null);
                    xiaofeiFragment s=new xiaofeiFragment();
                    ft.replace(R.id.framelayut,s);
                    MainActivity.now=s;
                    ft.commit();
                }
                if(i==4)
                {
                    FragmentTransaction ft=getFragmentManager().beginTransaction();
                    ft.addToBackStack(null);
                    jiesuanFragment s=new jiesuanFragment();
                    ft.replace(R.id.framelayut,s);
                    MainActivity.now=s;
                    ft.commit();
                }
                break;
        }
    }

    public class MyPagerAdapter extends PagerAdapter {
        public List<View> mListViews;

        public MyPagerAdapter(List<View> mListViews) {
            this.mListViews = mListViews;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(mListViews.get(arg1));
        }

        @Override
        public int getCount() {
            return mListViews.size();
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ViewGroup v = (ViewGroup) mListViews.get(arg1).getParent();
            if (v != null)
                v.removeView(mListViews.get(arg1));
            ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
            return mListViews.get(arg1);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }
    }

    public class MyOnClickListener implements View.OnClickListener {
        private int index;

        public MyOnClickListener(int index) {
            this.index = index;
        }

        @Override
        public void onClick(View view) {
            pager.setCurrentItem(this.index);
        }
    }

    public mainFragment() {
        drawer = MainActivity.drawer;
        nav_hander_layou = MainActivity.nav_hander_layou;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.app_bar_main, container, false);
        main_title = (LinearLayout) (view.findViewById(R.id.main_title));
        main_content = (LinearLayout) (view.findViewById(R.id.main_content));

        main_title_left = (ImageButton) view.findViewById(R.id.main_title_left);
        main_title_left.setOnClickListener(this);

        image_setting = (ImageView) view.findViewById(R.id.image_mine);
        image_time = (ImageView) view.findViewById(R.id.image_work);
        image_zhuye = (ImageView) view.findViewById(R.id.image_zhuye);

        pager = (ViewPager) view.findViewById(R.id.viewpager);
        listview = new ArrayList<View>();
        listview.add(inflater.inflate(R.layout.page1, null));
        listview.add(inflater.inflate(R.layout.page2, null));
        listview.add(inflater.inflate(R.layout.page3, null));
        pager.setAdapter(new MyPagerAdapter(listview));
        pager.setCurrentItem(0);
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    tab1.setTextColor(Color.parseColor("#FF0000"));
                    tab2.setTextColor(Color.parseColor("#000000"));
                    tab3.setTextColor(Color.parseColor("#000000"));
                    image_zhuye.setImageResource(R.drawable.zhuye_red);
                    image_setting.setImageResource(R.drawable.setting_blank);
                    image_time.setImageResource(R.drawable.clock_blank);
                }
                if (position == 1) {
                    tab2.setTextColor(Color.parseColor("#FF0000"));
                    tab1.setTextColor(Color.parseColor("#000000"));
                    tab3.setTextColor(Color.parseColor("#000000"));
                    image_zhuye.setImageResource(R.drawable.zhuye_blank);
                    image_time.setImageResource(R.drawable.clock_red);
                    image_setting.setImageResource(R.drawable.setting_blank);
                }
                if (position == 2) {
                    tab3.setTextColor(Color.parseColor("#FF0000"));
                    tab1.setTextColor(Color.parseColor("#000000"));
                    tab2.setTextColor(Color.parseColor("#000000"));
                    image_zhuye.setImageResource(R.drawable.zhuye_blank);
                    image_setting.setImageResource(R.drawable.setting_red);
                    image_time.setImageResource(R.drawable.clock_blank);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        tab1 = (TextView) view.findViewById(R.id.tab1);
        tab2 = (TextView) view.findViewById(R.id.tab2);
        tab3 = (TextView) view.findViewById(R.id.tab3);
        page1 = (LinearLayout) view.findViewById(R.id.page1);
        page2 = (LinearLayout) view.findViewById(R.id.page2);
        page3 = (LinearLayout) view.findViewById(R.id.page3);
        page1.setOnClickListener(new MyOnClickListener(0));
        page2.setOnClickListener(new MyOnClickListener(1));
        page3.setOnClickListener(new MyOnClickListener(2));
        /**
         * page3
         */
        MyPagerAdapter myadapter = (MyPagerAdapter) pager.getAdapter();
        LinearLayout page_3 = (LinearLayout) myadapter.instantiateItem(pager, 2);
        LinearLayout page_2 = (LinearLayout) myadapter.instantiateItem(pager, 1);
        gridview = (GridView) page_3.findViewById(R.id.page3_gridview);
        SimpleAdapter adpater = new SimpleAdapter(getActivity(),getData(),R.layout.set_item,new String[]{"draw","text"},new int[]{R.id.set_image,R.id.set_tv});
        gridview.setAdapter(adpater);
        gridview.setOnItemClickListener(this);

        time= (TextView) page_2.findViewById(R.id.work_time);
        return view;
    }

    /**
     * onStart()中初始化标题栏颜色
     */
    @Override
    public void onStart() {
        super.onStart();
        MainActivity.now=this;
        File file = new File("/data/data/com.example.andylau96.res/files/color.txt");
        try {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
            BufferedReader br = new BufferedReader(reader);
            theme = "";
            String temp = null;
            while ((temp = br.readLine()) != null) {
                theme += temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        main_title.setBackgroundColor(Color.parseColor(theme));
        nav_hander_layou.setBackgroundColor(Color.parseColor(theme));
    }

    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        if(MainActivity.permission==2) {
            map.put("draw", R.drawable.dingcan);
            map.put("text", "订餐");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.erji);
            map.put("text", "二级库存");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.dengji);
            map.put("text", "登记管理");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.xiaofeiguanli);
            map.put("text", "消费项目");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.jiesuan);
            map.put("text", "结算");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);
            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);
        }
        if(MainActivity.permission==1)
        {
            map = new HashMap<>();
            map.put("draw", R.drawable.taocanguanli);
            map.put("text", "套餐");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);
            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);
        }
        if(MainActivity.permission==5)
        {
            map = new HashMap<>();
            map.put("draw", R.drawable.gongyingshang);
            map.put("text", "供应商");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.jinhuo);
            map.put("text", "进货/退货");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);
            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);
            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

        }
        if(MainActivity.permission==4)
        {
            map = new HashMap<>();
            map.put("draw", R.drawable.caipu);
            map.put("text", "菜谱");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);
            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);
        }
        if(MainActivity.permission==3)
        {
            map = new HashMap<>();
            map.put("draw", R.drawable.yuangongxinxi);
            map.put("text", "员工管理");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.tianjia);
            map.put("text", "添加");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);
            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);

            map = new HashMap<>();
            map.put("draw", R.drawable.kongbai);
            map.put("text", " ");
            list.add(map);
        }
        return list;
    }
}
